<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">
      <el-button
        type="primary"
        @click="handleAdd">发布</el-button>
      <!--列表-->
      <el-table :data="DList" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index">
        </el-table-column>
        <el-table-column prop="exam" label="试卷名称" sortable width="160px">
        </el-table-column>
        <el-table-column prop="pObject" label="下发对象" sortable width="120px">
        </el-table-column>
        <el-table-column prop="time" label="考试时间" sortable width="120px">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="handleMonitor(scope.row)">查看进度</el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <!--<el-table-column prop="age" label="name数量" width="100" sortable>-->
        <!--</el-table-column>-->
      </el-table>

      <el-dialog title="试卷下发" :visible.sync="dialogFormVisible">
        <el-form :model="selectTable">
          <el-form-item label="试卷名称" label-width="100px">
            <el-select v-model="selectTable.exam_id" :placeholder="selectMenu.label">
              <el-option
                v-for="item in examOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="考试时间" label-width="100px" prop="perCount">
            <el-input v-model="selectTable.time" style="width: 200px">
              <template slot="append">分钟</template>
            </el-input>
          </el-form-item>
          <el-form-item label="下发对象" label-width="100px">
            <el-select v-model="selectTable.pObject_id" :placeholder="selectMenuO.label" filterable>
              <el-option
                v-for="item in objectOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="publishExam">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="提示"
        v-model="dialogFormVisible0"
      >
        <el-table :data="MList.data" highlight-current-row v-loading="loading" style="width: 100%;">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="仿真状态">
                  <span v-for="i in props.row.sim">
                    <div>{{ i.name }}:{{i.value}}</div>
                  </span>
                </el-form-item>
                <el-form-item label="硬件状态">
                  <span v-for="i in props.row.real">
                    <div>{{ i.name }}:{{i.data}}</div>
                  </span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="member" label="成员" sortable>
          </el-table-column>
          <el-table-column label="进度" sortable>
            <template slot-scope="props"><el-progress :percentage=props.row.step width="50px"></el-progress></template>
          </el-table-column>

        </el-table>


        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible0 = false">取 消</el-button>
    <el-button type="primary" @click="freshTask">刷新</el-button>
  </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>
  import headTop from './head.vue'
  //  import {baseUrl, baseImgPath} from '@/config/env'
  import {getExamList, getExamPublish, getObjectList, publishExam, publishDel, getVerifyResult} from '@/api/api'
  export default {
    data () {
      return {
        selectTable: {
          exam_id: '',
          time: '',
          pObject_id: ''
        },
        dialogFormVisible: false,
        dialogFormVisible0: false,
        examOptions: [],
        selectMenu: {},
        objectOptions: [],
        selectMenuO: {},
        DList: [],
        MList: {},
        selectIndex: null,
        selectIndexO: null
      }
    },
    components: {
      headTop
    },
    created () {
      this.initData()
    },
    methods: {
      // 获取设备列表
      async initData () {
        this.loading = true
        try {
          getExamPublish().then((res) => {
            this.DList = res.data
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      async getExamList () {
        this.examOptions = []
        try {
          getExamList().then((res) => {
            res.data.forEach((item, index) => {
              this.examOptions.push({
                label: item.name,
                value: item.id,
                index
              })
            })
          })
        } catch (err) {
          console.log('获取设备名称失败', err)
        }
      },
      async getObject () {
        this.objectOptions = []
        try {
          getObjectList().then((res) => {
            res.data.forEach((item, index) => {
              this.objectOptions.push({
                label: item.name,
                value: item.id,
                index
              })
            })
          })
        } catch (err) {
          console.log('获取设备名称失败', err)
        }
      },
      async getMonitor (row) {
        this.MList = {}
        try {
          getVerifyResult(row).then((res) => {
            this.MList = res.data
          })
        } catch (err) {
          console.log('获取设备名称失败', err)
        }
      },
      handleAdd () {
        this.getExamList()
        this.getObject()
        this.dialogFormVisible = true
      },
      handleMonitor (row) {
        this.dialogFormVisible0 = true
        this.getMonitor(row.id)
      },
      freshTask () {
        let row = this.MList.id
        this.getMonitor(row)
      },
      handleSelect (index) {
        this.selectIndex = index
        this.selectMenu = this.taskOptions[index]
      },
      handleSelectO (index) {
        this.selectIndexO = index
        this.selectMenuO = this.objectOptions[index]
      },
      async publishExam () {
        let postData = this.selectTable
        postData['User_id'] = 2
        publishExam(postData).then(res => {
          if (res.status === 200) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.dialogFormVisible = false
            this.initData()
          } else {
            this.$message({
              type: 'error',
              message: '添加失败'
            })
          }
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '添加失败'
          })
          console.log('发布失败', error)
        })
      },
      async handleDelete (index, row) {
        publishDel(row.id).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.DList.splice(index, 1)
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '删除失败,该任务已经发布，请先删除发布列表'
          })
          console.log('类型删除失败', error)
        })
      }
    }
  }
</script>

<style scoped lang="less">
  .el-button {
    margin: 5px;
  }
</style>
